package com.ciaojian.core.service;

import cn.hutool.core.map.MapUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ciaojian.core.enums.business.HairServiceTypeEnum;
import com.ciaojian.core.factory.ObjectMapperFactory;
import com.ciaojian.core.mapper.MemberMapper;
import com.ciaojian.core.model.Member;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.util.Map;

/**
 * @author Atlakyn
 */
public abstract class MemberService extends ServiceImpl<MemberMapper, Member> {

    /**
     * 根据服务类型获取会员折扣
     *
     * @param memberId 会员id
     * @param type     服务类型
     * @return 会员折扣
     * @throws JsonProcessingException json 转换异常
     */
    public String getDiscountByType(Integer memberId, HairServiceTypeEnum type) throws JsonProcessingException {
        String discount = new Member().selectById(memberId).getDiscount();
        ObjectMapper om = ObjectMapperFactory.getObjectMapperInstance();
        Map map = om.readValue(discount, Map.class);
        return MapUtil.getStr(map, type.getDescription());
    }

    /**
     * 对照两张会员卡的折扣大小
     *
     * @param v1 /
     * @param v2 /
     * @return 折扣会员卡
     * @throws JsonProcessingException /
     */
    public Member compareMember(Member v1, Member v2) throws JsonProcessingException {
        Map<String, Object> m1 = ObjectMapperFactory.getObjectMapperInstance().readValue(v1.getDiscount(), Map.class);
        Map<String, Object> m2 = ObjectMapperFactory.getObjectMapperInstance().readValue(v2.getDiscount(), Map.class);
        Double d1 = Double.valueOf(m1.get("剪发").toString());
        Double d2 = Double.valueOf(m2.get("剪发").toString());
        return d1.compareTo(d2) < 0 ? v1 : v2;
    }
}